'''

Case Type： 人为因素
Case Name： 修改pg_class表名称
Create At:  2020/10/12
@zou_jialiang0505328126
Descption: 1、新建表
           2、删除pg_class中该表名称
           3、对该表做数据插入，查询操作

history：
created： 2020/10/12  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys
import datetime

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()
commonshpri = CommonSH('PrimaryDbUser')

class ModifyConnection(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')    
    dbStandby1RootNode = Node(node='Standby1Root')    
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    Constant = Constant()
    CFE_PATH = macro.CFE_PATH
    TPCC_PATH = macro.TPCC_PATH

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info("-----------reliability_humanfactor002 start-----------")
        

    def test_modify_pidfile(self):
        logger.info("-----------create database--------------------")
        result = commonshpri.executDbSql('create database reliability_humanfactor002_database;')
        logger.info(result)
        self.assertIn(self.Constant.CREATE_DATABASE_SUCCESS_MSG, result)

        logger.info("-----------create table on reliability_humanfactor002_database--------------------")
        sql = 'create table reliability_humanfactor002(i int);'
        shell = '''
                source {env};
                gsql -d {db_name} -p {port} -r -c "{sql}"
                '''.format(env=self.DB_ENV_PATH, db_name='reliability_humanfactor002_database', port=self.dbPrimaryUserNode.db_port, sql=sql)
        logger.info(shell)
        msg = self.dbPrimaryUserNode.sh(shell).result()
        logger.info('executDbSql: ' + sql)
        logger.info(msg)
        self.assertIn(self.Constant.TABLE_CREATE_SUCCESS, msg)
        
        logger.info("-----------delete reliability_humanfactor002 from pg_class--------------------")
        sql = 'delete from pg_class where relname=\'reliability_humanfactor002\';'
        shell = '''
                       source {env};
                       gsql -d {db_name} -p {port} -r -c "{sql}"
                       '''.format(env=self.DB_ENV_PATH, db_name='reliability_humanfactor002_database',
                                  port=self.dbPrimaryUserNode.db_port, sql=sql)
        logger.info(shell)
        msg = self.dbPrimaryUserNode.sh(shell).result()
        logger.info('executDbSql: ' + sql)
        logger.info(msg)
        self.assertIn("DELETE", msg)
        
        logger.info("--------insert data into reliability_humanfactor002 fail---------")
        sql = 'insert into reliability_humanfactor002 values (1);'
        shell = '''
                       source {env};
                       gsql -d {db_name} -p {port} -r -c "{sql}"
                       '''.format(env=self.DB_ENV_PATH, db_name='reliability_humanfactor002_database',
                                  port=self.dbPrimaryUserNode.db_port, sql=sql)
        logger.info(shell)
        msg = self.dbPrimaryUserNode.sh(shell).result()
        logger.info('executDbSql: ' + sql)
        logger.info(msg)
        self.assertIn(self.Constant.NOT_EXIST, msg)

        

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info("-----------drop database--------------------")
        result = commonshpri.executDbSql('drop database if exists reliability_humanfactor002_database;')
        logger.info(result)
        
       